﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SGLL
{
    class ClassConsultation
    {
        //Variables
        private string _title = string.Empty;
        private string _sqlQuery = string.Empty;
        private readonly string[] _result = new string[3];
        //Objects
        private Form _formConsultation;
        private TableLayoutPanel _tableLayoutPanelConsultation;
        private TextBox _textBoxConsultation;
        private Label _labelTitle;
        private DataGridView _dataGridViewConsultation;
        //Properties
        /// <summary>
        /// Class Constructor
        /// </summary>
        /// <param name="title">Title of the Consultation</param>
        /// <param name="sqlQuery">Query to the DataBase to FillOut the Table</param>
        public ClassConsultation(string title, string sqlQuery)
        {
            _title = title;
            _sqlQuery = sqlQuery;
        }
        /// <summary>
        /// Show the Consultation Box
        /// </summary>
        /// <returns>Row Selected by the User</returns>
        public string[] Show()
        {
            //Local Variables
            DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
            DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
            Font font = new Font("Tahoma", 9F, FontStyle.Regular, GraphicsUnit.Point, 0);
            //Initialize
            _formConsultation = new Form();
            _tableLayoutPanelConsultation = new TableLayoutPanel();
            _labelTitle = new Label();
            _textBoxConsultation = new TextBox();
            _dataGridViewConsultation = new DataGridView();
            _tableLayoutPanelConsultation.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(_dataGridViewConsultation)).BeginInit();
            //SuspendLayout();
            // 
            // tableLayoutPanelConsultation
            // 
            _tableLayoutPanelConsultation.ColumnCount = 1;
            _tableLayoutPanelConsultation.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            _tableLayoutPanelConsultation.Controls.Add(_dataGridViewConsultation, 0, 2);
            _tableLayoutPanelConsultation.Controls.Add(_textBoxConsultation, 0, 1);
            _tableLayoutPanelConsultation.Controls.Add(_labelTitle, 0, 0);
            _tableLayoutPanelConsultation.Dock = DockStyle.Fill;
            _tableLayoutPanelConsultation.Location = new Point(0, 0);
            _tableLayoutPanelConsultation.Margin = new Padding(0);
            _tableLayoutPanelConsultation.Name = "tableLayoutPanelConsultation";
            _tableLayoutPanelConsultation.RowCount = 3;
            _tableLayoutPanelConsultation.RowStyles.Add(new RowStyle(SizeType.Absolute, 35F));
            _tableLayoutPanelConsultation.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
            _tableLayoutPanelConsultation.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            _tableLayoutPanelConsultation.Size = new Size(517, 332);
            _tableLayoutPanelConsultation.TabIndex = 3;
            // 
            // labelTitle
            // 
            _labelTitle.AutoSize = true;
            _labelTitle.BackColor = Color.SteelBlue;
            _labelTitle.Dock = DockStyle.Fill;
            _labelTitle.Font = new Font("Tahoma", 11.75F, FontStyle.Regular, GraphicsUnit.Point, 0);
            _labelTitle.ForeColor = Color.White;
            _labelTitle.Image = Properties.Resources.search_32x32;
            _labelTitle.ImageAlign = ContentAlignment.MiddleLeft;
            _labelTitle.Location = new Point(3, 0);
            _labelTitle.Name = "labelTitle";
            //_labelTitle.Size = new Size(511, 30);
            _labelTitle.TabIndex = 2;
            _labelTitle.Text = _title;
            _labelTitle.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // textBoxConsultation
            // 
            _textBoxConsultation.Dock = DockStyle.Fill;
            _textBoxConsultation.Location = new Point(3, 33);
            _textBoxConsultation.Name = "textBoxConsultation";
            _textBoxConsultation.Size = new Size(511, 26);
            _textBoxConsultation.TextChanged += _textBoxConsultation_TextChanged;
            _textBoxConsultation.KeyDown += _textBoxConsultation_KeyDown;
            _textBoxConsultation.KeyPress += _textBoxConsultation_KeyPress;
            _textBoxConsultation.TabIndex = 0;
            // 
            // dataGridViewConsultation
            // 
            _dataGridViewConsultation.AllowUserToAddRows = false;
            _dataGridViewConsultation.AllowUserToDeleteRows = false;
            _dataGridViewConsultation.AllowUserToResizeRows = false;
            dataGridViewCellStyle1.BackColor = Color.WhiteSmoke;
            dataGridViewCellStyle2.Font = font;
            _dataGridViewConsultation.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            _dataGridViewConsultation.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            _dataGridViewConsultation.BackgroundColor = Color.Silver;
            _dataGridViewConsultation.BorderStyle = BorderStyle.None;
            _dataGridViewConsultation.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            _dataGridViewConsultation.Dock = DockStyle.Fill;
            _dataGridViewConsultation.Location = new Point(3, 63);
            _dataGridViewConsultation.MultiSelect = false;
            _dataGridViewConsultation.Name = "dataGridViewConsultation";
            _dataGridViewConsultation.ReadOnly = true;
            _dataGridViewConsultation.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            _dataGridViewConsultation.Size = new Size(511, 266);
            _dataGridViewConsultation.KeyDown += _dataGridViewConsultation_KeyDown;
            _dataGridViewConsultation.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(_dataGridViewConsultation_RowHeaderMouseClick);
            _dataGridViewConsultation.ClearSelection();
            _dataGridViewConsultation.TabIndex = 1;
            // 
            // FormConsultation
            // 
            _formConsultation.AutoScaleDimensions = new SizeF(8F, 18F);
            _formConsultation.AutoScaleMode = AutoScaleMode.Font;
            _formConsultation.ClientSize = new Size(517, 332);
            _formConsultation.Controls.Add(_tableLayoutPanelConsultation);
            _formConsultation.Font = font;
            _formConsultation.ForeColor = Color.FromArgb(64, 64, 64);
            _formConsultation.FormBorderStyle = FormBorderStyle.FixedDialog;
            _formConsultation.Margin = new Padding(4, 4, 4, 4);
            _formConsultation.StartPosition = FormStartPosition.CenterParent;
            _formConsultation.Width = 850;
            _formConsultation.Height = 600;
            _formConsultation.MaximizeBox = false;
            _formConsultation.MinimizeBox = false;
            _formConsultation.Text = _title;
            _tableLayoutPanelConsultation.ResumeLayout(false);
            _tableLayoutPanelConsultation.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(_dataGridViewConsultation)).EndInit();
            _formConsultation.ShowDialog();
            return _result;
        }
        //Event Handlers
        private void _textBoxConsultation_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !ClassFunctions.TextInputFilter(ClassFunctions.Filters.Alphanumeric, e.KeyChar);
        }
        private void _dataGridViewConsultation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                fillArray();
                _formConsultation.Dispose();
            }
        }
        private void _dataGridViewConsultation_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            fillArray();
            _formConsultation.Dispose();
        }
        private void _textBoxConsultation_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            { 
                case Keys.Enter:
                    fillArray();
                    _formConsultation.Dispose();
                    break;
                case Keys.Escape:
                    _formConsultation.Close();
                    break;

            }
        }
        private void _textBoxConsultation_TextChanged(object sender, EventArgs e)
        {
            _dataGridViewConsultation.DataSource = ClassFunctions.GetData(string.Format(_sqlQuery, _textBoxConsultation.Text.Trim()));
        }
        //General Purpose-Functions
        private void fillArray()
        {
            _result[0] = _dataGridViewConsultation[0, _dataGridViewConsultation.SelectedRows[0].Index].Value.ToString();
            _result[1] = _dataGridViewConsultation[1, _dataGridViewConsultation.SelectedRows[0].Index].Value.ToString();
            _result[2] = _dataGridViewConsultation[3, _dataGridViewConsultation.SelectedRows[0].Index].Value.ToString();
        }
    }
}